﻿using MQTTnet.Server;
using System;
using System.Collections.Generic;
using System.Text;
using adb;
using Microsoft.EntityFrameworkCore;
using SensorCollection.AppCode;
using Masuit.Tools;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace SensorCollection
{

    public class SignalOutJsonModel
    {
        [JsonPropertyName("out")]
        public decimal _out { get; set; }
    }

    /// <summary>
    /// 晴雨表数据上传
    /// </summary>
    public class SignalOut : IDataDealwith
    {
        private MqttServer mqttServer;
        private DbContextOptionsBuilder<DeviceDbContext> optionsBuilder;

        public DbContextOptionsBuilder<MyDbContext> MyOptionsBuilder { get; }

        public SignalOut(MqttServer mqttServer, DbContextOptionsBuilder<DeviceDbContext> optionsBuilde, DbContextOptionsBuilder<MyDbContext> MyOptionsBuilder)
        {
            this.mqttServer = mqttServer;
            this.optionsBuilder = optionsBuilde;
            this.MyOptionsBuilder = MyOptionsBuilder;
        }

        public long keyId => 820089;

        public object JsonToModel(string json, string deviceSN = null)
        {
            return System.Text.Json.JsonSerializer.Deserialize<SignalOutJsonModel>(json);
        }
        public async Task ReactData(object obj, string Topic, string deviceSN = null)
        {
            try
            {
                SignalOutJsonModel obj1 = obj as SignalOutJsonModel;
                if (obj1 == null) return;
                if (Topic.IsNullOrEmpty()) return;

                if ("wsr".Equals(deviceSN))
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSignalOut_WSR.AddAsync(new tSignalOut_WSR
                        {
                            Topic = Topic,
                            Data = obj1._out,
                            addTime = DateTime.Now
                        });
                        await db.SaveChangesAsync();
                    }
                }
                else if ("YKF2".Equals(deviceSN))
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSignalOutData_YKF2.AddAsync(new tSignalOutData_YKF2
                        {
                            Topic = Topic,
                            Data = obj1._out,
                            addTime = DateTime.Now
                        });
                        await db.SaveChangesAsync();
                    }
                }
                else if ("YKF1".Equals(deviceSN))
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSignalOutData_YKF1.AddAsync(new tSignalOutData_YKF1
                        {
                            Topic = Topic,
                            Data = obj1._out,
                            addTime = DateTime.Now
                        });
                        await db.SaveChangesAsync();
                    }
                }
                else if ("XAFD".Equals(deviceSN))
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSingleSignal_XAFD.AddAsync(new tSingleSignal_XAFD
                        {
                            Topic = Topic,
                            Data = obj1._out,
                            addTime = DateTime.Now
                        });
                        await db.SaveChangesAsync();
                    }
                }
                else
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSingleSignal.AddAsync(new tSingleSignal
                        {
                            Topic = Topic,
                            Data = obj1._out,
                            addTime = DateTime.Now
                        });
                        await db.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("SignalOutJsonModel:" + ex.Message);
            }
        }

    }
}
